//
//  DLPurchaseVC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/20.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLPurchaseVC.h"


@interface DLPurchaseVC ()<UITextFieldDelegate>

@property (nonatomic, assign) BOOL isSelectedProtocol;

@property (nonatomic, strong) UITextField *textField;
@end

@implementation DLPurchaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNav];
    [self layoutSubControls];
    // Do any additional setup after loading the view.
}
- (void) layoutSubControls {
    UIView *bgView = [UIView new];
    bgView.frame = CGRectMake(0, 10, kScreenWidth, 44);
    [self.view addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(0, 0, 80, bgView.height);
    titleLabel.text = @"买入金额";
    titleLabel.font = kSystemFont(14);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = kDLNormalBlackColor;
    [bgView addSubview: titleLabel];
    //    http://t.cn/EUhn0b3    房间密码：666
    
    UITextField *textField = [UITextField new];
    textField.frame = CGRectMake(titleLabel.right, titleLabel.top, kScreenWidth - titleLabel.right - kAutoWidth(20), titleLabel.height);
    textField.delegate = self;
    textField.font = kSystemFont(14);
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.placeholder = @"请输入买入金额";
    [bgView addSubview:textField];
    self.textField = textField;
    [textField addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
    
    UIView *alertBgView = [UIView new];
    alertBgView.frame = CGRectMake(bgView.left, bgView.bottom + 1, bgView.width, bgView.height / 2);
    alertBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:alertBgView];
    
    UILabel *alertLabel = [UILabel new];
    alertLabel.frame = CGRectMake(10, 0, alertBgView.width - 20, alertBgView.height);
    alertLabel.backgroundColor = [UIColor whiteColor];
    alertLabel.font = kSystemFont(12);
    alertLabel.text = @"预估费率：0.2%，2000元起购";
    alertLabel.textColor = kColorRGB(204, 204, 204);
    alertLabel.textAlignment = NSTextAlignmentLeft;
    [alertBgView addSubview:alertLabel];
    
    CGFloat registBtnWidth = kAutoWidth(340);
    CGFloat leftMargin = (kScreenWidth - registBtnWidth) / 2;
    
    UIButton *redeemSelectButton = [UIButton buttonWithType:0];
    redeemSelectButton.frame = CGRectMake(leftMargin, bgView.bottom + 75, registBtnWidth, 44);
    redeemSelectButton.layer.cornerRadius = 4;
    redeemSelectButton.clipsToBounds = YES;
    redeemSelectButton.backgroundColor = kDLBtnNormalBgColor;
    [redeemSelectButton setTitle:@"确认购买" forState:UIControlStateNormal];
    redeemSelectButton.titleLabel.font = kSystemFont(14);
    [self.view addSubview:redeemSelectButton];
    [redeemSelectButton addTarget:self action:@selector(redeemSelectButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *protocolButton = [UIButton buttonWithType:0];
    protocolButton.frame = CGRectMake(leftMargin, redeemSelectButton.bottom + 10, 20, 20);
    protocolButton.adjustsImageWhenHighlighted = NO;
    [protocolButton setImage:[UIImage imageNamed:@"login_checkbox_no"] forState:UIControlStateNormal];
    [protocolButton setImage:[UIImage imageNamed:@"login_checkbox_yes"] forState:UIControlStateSelected];
    [self.view addSubview:protocolButton];
    [protocolButton addTarget:self action:@selector(protocolButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *protocolLabel = [UILabel new];
    protocolLabel.frame = CGRectMake(protocolButton.right + 5, protocolButton.top, kScreenWidth / 2, protocolButton.height);
    protocolLabel.text = @"我已阅读并同意《用户购买协议》";
    protocolLabel.textAlignment = NSTextAlignmentLeft;
    protocolLabel.textColor = kDLNormalGrayColor;
    protocolLabel.font = kSystemFont(12);
    [self.view addSubview:protocolLabel];
    
    NSMutableAttributedString *mutAttr = [[NSMutableAttributedString alloc] initWithString:protocolLabel.text];
    [mutAttr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(7, 8)];
    protocolLabel.attributedText = mutAttr;
}

- (void) textFieldValueChanged:(UITextField *)txtF {
    
    if (txtF.text.length==2 && [txtF.text hasPrefix:@"0"] && [[txtF.text substringFromIndex:1] isEqualToString:@"."]==NO) {
        txtF.text = [txtF.text substringFromIndex:1];
    }
    
//    CGFloat money = 1000;
//    //这个金额需要服务器返回
//    if (txtF.text.floatValue > money) {
//
//        kShowRequestMsg(@"输入金额超出可提现余额");
//        txtF.text = [NSString stringWithFormat:@"%.2f", money];
//    }
}

//textField.text 输入之前的值 string 输入的字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //删除处理
    if ([string isEqualToString:@""]) {
        return YES;
    }
    //首位不能为.号
    if (range.location == 0 && [string isEqualToString:@"."]) {
        return NO;
    }
    return [self isRightInPutOfString:textField.text withInputString:string range:range];
}
- (BOOL)isRightInPutOfString:(NSString *) string withInputString:(NSString *) inputString range:(NSRange) range{
    //判断只输出数字和.号
    NSString *passWordRegex = @"[0-9\\.]";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    if (![passWordPredicate evaluateWithObject:inputString]) {
        return NO;
    }
    //逻辑处理
    if ([string containsString:@"."]) {
        if ([inputString isEqualToString:@"."]) {
            return NO;
        }
        NSRange subRange = [string rangeOfString:@"."];
        if (range.location - subRange.location > 2) {
            return NO;
        }
    }
    return YES;
}
- (void) protocolButtonClick:(UIButton *)button {
    button.selected = !button.selected;
    self.isSelectedProtocol = !self.isSelectedProtocol;
}
- (void) redeemSelectButtonClick {
    
    if (self.textField.text.length < 1) {
        kShowRequestMsg(@"请输入买入金额");
        return;
    }
    
    if (!self.isSelectedProtocol) {
        kShowRequestMsg(@"请您阅读并同意《用户购买协议》");
        return;
    }
}
- (void) setUpNav {
    self.titleView.title = @"买入";
    self.view.backgroundColor = kColorRGB(240, 240, 240);
}
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
